//
//  QChargeBookVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/15.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QChargeBookVC.h"
#import "QChargeBookCell.h"
#import "QChargeBookView.h"
#import "QChargeBookModel.h"
#import "QEditChargeBookVC.h"
#import "QChargeBookDB.h"
#import "QChargeBookShow.h"
@interface QChargeBookVC ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation QChargeBookVC{
    NSMutableArray *_allData;//所有数据
    UITableView *_tableView;
    NSMutableArray *_isShowArr;
    QChargeBookCell *_chargeCell;
}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"全部明细";
    _allData = [NSMutableArray array];
    _isShowArr = [NSMutableArray array];
    [self createTableView];//创建tableview
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAction) name:kNotificationShowData object:nil];
    [self notiAction];
}

- (void)notiAction{
    NSArray *arr = [QChargeBookDB queryChargeBookData];
    _allData = [QChargeBookDB setupChargeBookData:arr];
    [_isShowArr removeAllObjects];
    for (int i=0; i<_allData.count; ++i) {
        QChargeBookShow *show = [[QChargeBookShow alloc] init];
        [_isShowArr addObject:show];
    }
    [_tableView reloadData];
}

- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:@"QChargeBookCell" bundle:nil] forCellReuseIdentifier:@"QChargeBookCell"];
    _chargeCell = [_tableView dequeueReusableCellWithIdentifier:@"QChargeBookCell"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 45, 45);
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    [button setImage:[UIImage imageNamed:@"newAdd_image"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)addButtonAction{
    CLog(@"添加");
    QEditChargeBookVC *vc = [[QEditChargeBookVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _allData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *monthArr = _allData[section];
    QChargeBookShow *show = _isShowArr[section];
    if(show.isShow){
        return monthArr.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QChargeBookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QChargeBookCell" forIndexPath:indexPath];
    cell.indexPath = indexPath;
    cell.monthArr = _allData[indexPath.section];
    cell.model =  _allData[indexPath.section][indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    QChargeBookView *view = [[[NSBundle mainBundle] loadNibNamed:@"QChargeBookView" owner:self options:nil] lastObject];
    view.tagBtn.tag = section;
    NSArray *arr = _allData[section];
    if(arr.count){
       view.arrData = arr;//一个月的数据
    }
    [view.tagBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    QChargeBookShow *show = _isShowArr[section];
    if(!show.isShow){
        view.showImage.image = [UIImage imageNamed:@"show_bottom"];
    }else{
        view.showImage.image = [UIImage imageNamed:@"show_top"];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 110;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    _chargeCell.indexPath = indexPath;
    _chargeCell.monthArr = _allData[indexPath.section];//一个月的数据
    _chargeCell.model = _allData[indexPath.section][indexPath.row];
    return [_chargeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
}

//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        NSMutableArray *arr = (NSMutableArray *)_allData[indexPath.section];
//        [arr removeObjectAtIndex:indexPath.row];
//        if(arr.count == 0){
//            [_isShowArr removeObjectAtIndex:indexPath.section];
//            [_allData removeObjectAtIndex:indexPath.section];
//            [_tableView reloadData];
//        }else{
//             [_tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
//        }
//    }
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSArray *monthArr = _allData[indexPath.section];//一个月的数据
    QChargeBookModel *model = monthArr[indexPath.row];
    QEditChargeBookVC *vc = [[QEditChargeBookVC alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)buttonAction:(UIButton *)button{
    QChargeBookShow *show = _isShowArr[button.tag];
    show.isShow = !show.isShow;
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag] withRowAnimation:UITableViewRowAnimationFade];
}

@end
